//
// Description: 502. IPO
// Created by Loading on 2021/9/8.
//

#include <bits/stdc++.h>

using namespace std;

int findMaximizedCapital(int k, int w, vector<int> &profits, vector<int> &capital) {
    int n = profits.size();
    vector<pair<int, int>> vec;
    for (int i = 0; i < n; ++i) {
        vec.emplace_back(capital[i], profits[i]);
    }
    sort(vec.begin(), vec.end());

    priority_queue<int> q;
    int index = 0;
    while (k--) {
        // 将所有启动最小资本小于等于当前资本的项目利润 加入大顶堆
        while (index < n && vec[index].first <= w) {
            q.emplace(vec[index++].second);
        }

        if (!q.empty()) {
            // 最大的利润，加入当前资本中
            w += q.top();
            q.pop();
        } else {
            // 已满足不了所有项目的最小启动资本
            break;
        }
    }

    return w;
}

int main() {
    int k = 2;
    int w = 0;
    vector<int> profits = {1, 2, 3};
    vector<int> capital = {0, 1, 1};
    int res = findMaximizedCapital(k, w, profits, capital);
    cout << res << endl;

    return 0;
}